package com.fractalist.man.base;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.fractalist.base.cache.Cache;

/**
 * 默认的page对象，可以根据传入的基本信息，封装出对应的page对象<br/>
 * 针对目前的整体框架设计，进行了page对象的封装，所有的构造对象调用使用的tableservice都必须继承IBaseService<br/>
 * 
 * @author samoin
 * @since 2013-11-26
 * 
 */
public class Page {
	private Integer pageNo;
	private Integer pageSize;
	private Integer totalCount;
	private Integer totalPage;
	private List<Map<String, Object>> list;
	private String tableName;
	private String conditions;
	private String expire;
	private IBaseService tableService;

	/**
	 * 默认构造一个空的page对象，里面的一些基本信息都为0
	 */
	public Page() {
		this.list = new ArrayList<Map<String, Object>>();
		this.totalCount = 0;
		this.totalPage = 0;
	}

	/**
	 * 默认的构造page对象的方法，通过传入的service，进行相关sql语句的调用，生成相关的信息存入page对象中，如果不需要缓存的话，
	 * 将expire传入“0s”即可<br/>
	 * <font color=red><b>注：所有传入的tableService必须继承IBaseService</b></font>
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @param tableName
	 * @param conditions
	 * @param expire
	 * @param tableService
	 */
	@SuppressWarnings("unchecked")
	public Page(Integer pageNo, Integer pageSize, String tableName,
			String conditions, String expire, IBaseService tableService) {
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.tableName = tableName;
		this.conditions = conditions;
		this.expire = expire;
		this.tableService = tableService;
		if (expire == null || expire.trim().length() == 0) {
			expire = "0s";
		}
		String countSql = SqlUtils.genderQuerySql(tableName,
				conditions, "count(*) as total");
		String key = "cache_countsql_" + tableName + "_" + conditions;
		Map<String, Object> map = (Map<String, Object>) Cache.get(key);
		if (map == null) {
			map = tableService.queryOneRow(countSql);
			Cache.add(key, map, expire);
		}
		if (map != null && map.size() > 0) {
			this.totalCount = Integer.valueOf(map.get("total") + "");
		} else {
			this.totalCount = 0;
		}
		this.totalPage = (this.totalCount % this.pageSize == 0) ? (this.totalCount / this.pageSize)
				: ((this.totalCount / this.pageSize) + 1);
		String listSql = SqlUtils.genderQuerySql(tableName,
				conditions, "*")
				+ " limit "
				+ ((pageNo - 1) * pageSize)
				+ ","
				+  pageSize;
		String key2 = "cache_listsql_" + tableName + "_" + conditions;
		List<Map<String, Object>> tableList = (List<Map<String, Object>>) Cache
				.get(key2);
		if (tableList == null) {
			tableList = tableService.queryList(listSql);
			Cache.add(key2, tableList, expire);
		}
		this.list = tableList;
	}

	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}

	public Integer getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}

	public List<Map<String, Object>> getList() {
		return list;
	}

	public void setList(List<Map<String, Object>> list) {
		this.list = list;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getExpire() {
		return expire;
	}

	public void setExpire(String expire) {
		this.expire = expire;
	}

	public IBaseService getTableService() {
		return tableService;
	}

	public void setTableService(IBaseService tableService) {
		this.tableService = tableService;
	}

	public String getConditions() {
		return conditions;
	}

	public void setConditions(String conditions) {
		this.conditions = conditions;
	}
}
